package com.aramco.fion.controller
{

	import com.aramco.fion.event.ManageTablesRecordsEvent;
	import com.aramco.fion.service.ManageTablesDelegate;
	import com.aramco.fion.util.BSPAdminDataModelOO;
	import com.aramco.fion.util.QueryUtil;
	
	import ext.swizframework.utils.AsyncInterceptor;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.events.ItemClickEvent;
	import mx.logging.ILogger;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.swizframework.utils.services.ServiceHelper;


	public class ManageTablesRecordsController
	{
		
		[Inject]
		public var serviceRequestUtil:ServiceHelper;
		
		[Log]			
		public var log  : ILogger = null;	
		
		[Inject]
		public var baseController:BaseController;
		
		[Inject]
		public var delegate:ManageTablesDelegate;
		
		private var currentEvent:Event;

		/**
		 * Swiz will automatically create any Bean for of the built-in helper classes
		 * if one has not been defined.
		 */ 
		//public var serviceHelper : ServiceHelper;
		
		public function ManageTablesRecordsController()
		{
			
		}
		
		//---------------- Load F4 Variables ---------------------------------

		[EventHandler(event = "ManageTablesRecordsEvent.GET_ALL_RECORDS_EVENT")]
		public function getAll(event:ManageTablesRecordsEvent):void
		{
			baseController.cacheCaller(event);
			currentEvent  = event;
			//var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getDashboardTableRecords);
			var call:AsyncToken = delegate.getAll(event.tableName);
			//var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getF4Values);
			serviceRequestUtil.executeServiceCall(call, loadResultHandler, loadFaulttHandler );
		
		}
		
		
		[EventHandler(event = "ManageTablesRecordsEvent.CREATE_RECORD_EVENT")]
		public function createRecord(event:ManageTablesRecordsEvent):void
		{
			/*	currentEvent  = event;
			var call:AsyncToken = delegate.loadF4TRS(event.parm);
			var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getF4Values);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
			*/
		}
		
		
		[EventHandler(event = "ManageTablesRecordsEvent.GET_BY_KEY_EVENT")]
		public function getRecordById(event:ManageTablesRecordsEvent):void
		{
			/*	currentEvent  = event;
			var call:AsyncToken = delegate.loadF4TRS(event.parm);
			var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getF4Values);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
			*/
		}
		
		[EventHandler(event = "ManageTablesRecordsEvent.UPDATE_BY_KEY_EVENT")]
		public function updateRecordById(event:ManageTablesRecordsEvent):void
		{
			/*	currentEvent  = event;
			var call:AsyncToken = delegate.loadF4TRS(event.parm);
			var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getF4Values);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
			*/
		}
		
		[EventHandler(event = "ManageTablesRecordsEvent.DEACTIVATE_BY_KEY_EVENT")]
		public function deactivateRecordById(event:ManageTablesRecordsEvent):void
		{
			/*	currentEvent  = event;
			var call:AsyncToken = delegate.loadF4TRS(event.parm);
			var agent : AsyncInterceptor = new AsyncInterceptor(QueryUtil.getF4Values);
			serviceRequestUtil.executeServiceCall( agent.intercept(call), loadResultHandler, loadFaulttHandler );
			*/
		}

	

		
		
		//------------ Handel Result ---------------------------------
		private function loadResultHandler(data: * ):void 
		{
			if(currentEvent.type == ManageTablesRecordsEvent.DEACTIVATE_BY_KEY_EVENT)
			{
				if (log != null) log.debug("loadResultHandler(data: * )");
				baseController.notifyCaller(data);
			}
			
			else if(currentEvent.type == ManageTablesRecordsEvent.GET_ALL_RECORDS_EVENT)
			{
				baseController.notifyCaller( new BSPAdminDataModelOO(data.result as XML));
			}
			
			else if(currentEvent.type == ManageTablesRecordsEvent.GET_BY_KEY_EVENT)
			{
				baseController.notifyCaller(data);
			}
			
			else if(currentEvent.type == ManageTablesRecordsEvent.UPDATE_BY_KEY_EVENT)
			{
				baseController.notifyCaller(data);
			}
			
			else if(currentEvent.type == ManageTablesRecordsEvent.CREATE_RECORD_EVENT)
			{
			//	trace(((data as XML)..VARIABLES).children());
				baseController.notifyCaller(data);
			}
		}
		
		//------------ Handel Fault ---------------------------------
		private function loadFaulttHandler(event:FaultEvent):void
		{
			trace(event);
		}
	}
}